package com.wframe.project.jnd.wx.filter;

import com.wframe.project.jnd.rest.util.ClientException;
import com.wframe.project.jnd.rest.util.CodeEnum;
import com.wframe.project.jnd.rest.util.JsonReturn;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import java.io.IOException;

/**
 * Created by lwb on 2017/4/19.
 */
@Component
public class WechatHandlerExceptionResolver extends AbstractHandlerExceptionResolver {

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView mv = new ModelAndView();
        String uri = request.getRequestURI();
        String accept = request.getHeader("accept");
        String header = request.getHeader("X-Requested-With");
        if (uri != null) {
            if (uri.indexOf("/we/") > -1) {
                if ((accept != null && accept.indexOf(MediaType.APPLICATION_JSON) > -1) || (header != null && header.indexOf("XMLHttpRequest") > -1)) {
                    // json 请求返回
                    if (!(ex instanceof ClientException)) {
                        ex = new ClientException(CodeEnum.Err);
                    }

                    response.setStatus(HttpStatus.OK.value());
                    response.setContentType(MediaType.APPLICATION_JSON);
                    response.setHeader("Cache-Control", "no-cache, must-revalidate");
                    try {
                        response.getWriter().write(new JsonReturn().toErrorString(getClass(), ex));
                    } catch (IOException e) {
                        logger.error("", ex);
                    }
                } else {
                    mv = new ModelAndView("/we/message");
                }
            } else {
                if ((accept != null && accept.indexOf(MediaType.APPLICATION_JSON) > -1) || (header != null && header.indexOf("XMLHttpRequest") > -1)) {
                    // json 请求返回
                    if (!(ex instanceof ClientException)) {
                        ex = new ClientException(CodeEnum.Err);
                    }

                    response.setStatus(HttpStatus.OK.value());
                    response.setContentType(MediaType.APPLICATION_JSON);
                    response.setHeader("Cache-Control", "no-cache, must-revalidate");
                    try {
                        response.getWriter().write(new JsonReturn().toErrorString(getClass(), ex));
                    } catch (IOException e) {
                        logger.error("", ex);
                    }
                } else {
                    mv = new ModelAndView("/website/message", "error", ex.getMessage());
                }
            }
        }
        return mv;
    }

}
